package com.goparry.taskman.repository;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class RObject implements Comparable<RObject>{
	static SimpleDateFormat FORMATTER_SET = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	static SimpleDateFormat FORMATTER_GET = new SimpleDateFormat("MM/dd/yyyy");
	private String ID;
	private String name;
	private String title;
	private String format;
	private long size;
	private Date dateLastModified;
	private String status;
	private String revision;
	private String htmlProperties;
	private boolean isContainer;
	public static SortBy sortBy;
	public enum SortBy { NAME, DATE, STATUS }
	
	public RObject(String ID, String name, String title, String format, String dateLastModified, String status, String revision, String htmlProperties){
		super();
		this.ID = ID;
		this.name = name;
		this.title = title;
		this.format = format;
		setDateLastModified(dateLastModified);
		this.status = status;
		this.revision = revision;
		this.htmlProperties = htmlProperties;
	}

	public RObject copy() {
		return new RObject(getID(), getName(), getTitle(), getFormat(), getDateLastModified(), getStatus(), getRevision(), getHtmlProperties());
	}
	public RObject() {
		super();
	}

	@Override
	public String toString() {
		return name;
	}

	public String getID() {
		return ID;
	}

	public void setID(String iD) {
		ID = iD;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	public String getDateLastModified() {
		return FORMATTER_GET.format(dateLastModified);
	}

	public Date getDateLastModifiedAsDate() {
		return dateLastModified;
	}

	public void setDateLastModified(String dateLastModified) {
		try {
			this.dateLastModified = FORMATTER_SET.parse(dateLastModified.trim());
		} catch (ParseException e) {
			this.dateLastModified = null;
		}
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getRevision() {
		return revision;
	}

	public void setRevision(String revision) {
		this.revision = revision;
	}

	public String getHtmlProperties() {
		return htmlProperties;
	}

	public void setHtmlProperties(String htmlProperties) {
		this.htmlProperties = htmlProperties;
	}
	
	public boolean getIsContainer() {
		return isContainer;
	}
	
	public void setIsContainer(boolean isContainer) {
		this.isContainer = isContainer;
	}
	
	public SortBy getSortBy() {
		return sortBy;
	}

	public void setSortBy(SortBy sortBy) {
		RObject.sortBy = sortBy;
	}


	@Override
	public int compareTo(RObject another) {
		if (isContainer && another.isContainer) { // Both Folders
			return name.compareToIgnoreCase(another.getName());
		} else if (isContainer) { // This is a folder but another is not
			return -1;
		} else if (another.getIsContainer()) {
			return 1;
		}
		switch (sortBy) {
		case NAME:
			return name.compareToIgnoreCase(another.getName());
		case DATE:
			return dateLastModified.compareTo(another.getDateLastModifiedAsDate());
		case STATUS:
			return status.compareToIgnoreCase(another.getStatus());
		default:
			return 0;
		}
	}

	public long getSize() {
		return size;
	}

	public void setSize(long size) {
		this.size = size;
	}
	public String getSizeAsString() {
		return String.valueOf(size);
	}

	public void setSizeAsString(String sSize) {
      try {
    	  this.size = Long.parseLong(sSize.trim());
       } catch (NumberFormatException nfe) {
    	   this.size = -1;
       }
	}
}

